package com.hxkcloud.nurse.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.beans.ReportValueBean;
import com.hxkcloud.nurse.interfaces.OnRefreshLayoutCanScroll;
import com.hxkcloud.nurse.utils.ImageOptionUtils;
import com.hxkcloud.nurse.views.SwipeLayout;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * ProjectName: YJHCuzhong
 * PackageName: com.yjhcloud.stroke.adapter
 * Description:
 * Create by Ray.
 * Date: 2016/10/8 21:03
 */
public class ReportDetailListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    private Context mContext;
    private List<ReportValueBean> datas;
    private RecyclerView recyclerview;
    private LayoutInflater mInflater;

    private ArrayList<SwipeLayout> mOpenItems = new ArrayList<SwipeLayout>();

    private PostOptionsListener optionsListener;

    private String userStyle = "";

    private ImageLoader imageLoader;

    private double maxValue = 0;
    private double minValue = 0;


    public ReportDetailListAdapter(Context mContext, List<ReportValueBean> datas,
                             RecyclerView recyclerview,String userStyle,PostOptionsListener optionsListener) {
        this.mContext = mContext;
        this.datas = datas;
        this.recyclerview = recyclerview;
        this.userStyle = userStyle;
        this.optionsListener = optionsListener;
        mInflater = LayoutInflater.from(mContext);
        imageLoader = ImageLoader.getInstance();
    }

    public void setReferMaxValue(String max){
        maxValue = Double.parseDouble(max);
    }

    public void setReferMinValue(String min){
        minValue = Double.parseDouble(min);
    }

    public void setData(List<ReportValueBean> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    public void clearData() {
        datas.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        // TODO Auto-generated method stub
        return datas.size();
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        // TODO Auto-generated method stub


        final Holder mHolder = (Holder) holder;
        final ReportValueBean bean = datas.get(position);
        final ArrayList<String> lsit = new ArrayList<>();
        lsit.add(bean.getPic());
        String resultTime = getDateFormat("yyyy-MM-dd HH:mm", bean.getTime());
        if(resultTime.contains("-")){
            String time = resultTime.replace("-","/");
            mHolder.timeTV.setText(time);
        }
        if("".equals(bean.getPic())){
            mHolder.picIV.setVisibility(View.INVISIBLE);
        }else{
            mHolder.picIV.setVisibility(View.VISIBLE);
            imageLoader.displayImage(bean.getPic(), mHolder.picIV, ImageOptionUtils.getInstance().getSimplePicOptions());
        }
        mHolder.picIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                optionsListener.PostOptions(mHolder.picIV, position, "displayImg");

//                Intent intent = new Intent(mContext, DisplayImageActivity.class);
//                intent.putExtra("path", bean.getPic());
//
//                intent.putExtra("title", position);
//                intent.putExtra("position", position);
//                intent.putStringArrayListExtra("pics", lsit);
//                intent.putExtra("isHideDelete", "true");
//                mContext.startActivity(intent);
//                lsit.removeAll(lsit);

            }
        });
        if(bean.getValue()!=null){
            double value = Double.parseDouble(bean.getValue());
            if(value>=minValue && value<=maxValue){
                mHolder.value1TV.setText(bean.getValue()+" ");
                mHolder.value1TV.setTextColor(mContext.getResources().getColor(R.color.grey_text1));
            }else if(value>maxValue){
                mHolder.value1TV.setText(bean.getValue()+"↑");
                mHolder.value1TV.setTextColor(mContext.getResources().getColor(R.color.red_point));
            }else if(value<minValue){
                mHolder.value1TV.setText(bean.getValue()+"↓");
                mHolder.value1TV.setTextColor(mContext.getResources().getColor(R.color.red_point));
            }

        }

        //必须添加的点击事件
        mHolder.ll_main.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mOpenItems.size() > 0) {
                    closeAll();
                } else {

                }
            }
        });
        //必须添加的点击事件
        mHolder.ll_back.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (mOpenItems.size() > 0) {
                    closeAll();
                }
            }
        });
        //编辑
        mHolder.tv_item_edit.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mOpenItems.size() > 0) {
                    closeAll();
                }
                optionsListener.PostOptions(mHolder.tv_item_edit, position, "edit");
            }
        });
        //删除
        mHolder.tv_item_delete.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mOpenItems.size() > 0) {
                    closeAll();
                }
                optionsListener.PostOptions(mHolder.tv_item_delete, position, "delete");
            }
        });

        mHolder.sl_item.setStopPulltorefreshScrollerListener(new SwipeLayout.StopPulltorefreshScrollerListener() {

            @Override
            public void setCanScroll(boolean canScoller) {
                // TODO Auto-generated method stub
                if (mCanScrollListener != null) {
                    mCanScrollListener.setCanScrollMove(canScoller);
                }
            }
        });

        mHolder.sl_item.setmOnStopListener(new SwipeLayout.OnStopListener() {

            @Override
            public boolean onStop(SwipeLayout layout) {
                if("2".equals(userStyle)){//护士
                    return false;
                }else if("3".equals(userStyle)){//患者
                    if(position==0){
                        return  true;
                    }

                }else if("4".equals(userStyle)){//医生
                    return  false;

                }
                return false;
            }

        });

        //管理侧滑swipelayout，只允许同时有一个侧滑弹出
        mHolder.sl_item.setmOnDragStateChangeListener(new SwipeLayout.OnDragStateChangeListener() {


            @Override
            public void onStartOpen(SwipeLayout layout) {
                closeAll();
                mOpenItems.add(layout);
            }

            @Override
            public void onStartClose(SwipeLayout layout) {

            }

            @Override
            public void onOpen(SwipeLayout layout) {

            }

            @Override
            public void onDragging(SwipeLayout layout) {


            }

            @Override
            public void onClose(SwipeLayout layout) {
                mOpenItems.remove(layout);
            }

        });



    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


        View v = mInflater.inflate(R.layout.item_reportdetaillist_layout, null);
        RecyclerView.ViewHolder holer = new Holder(v);

        return holer;
    }

    public void closeAll() {
        for (SwipeLayout item : mOpenItems) {
            item.close(true);
        }
    }

    @Override
    public int getItemViewType(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    private OnRefreshLayoutCanScroll mCanScrollListener;

    public void setOnRefreshLayoutCanScroll(OnRefreshLayoutCanScroll l) {
        mCanScrollListener = l;
    }

    public void setPostOptionsListener(PostOptionsListener optionsListener){
        this.optionsListener = optionsListener;
    }

    public interface PostOptionsListener{
        public void PostOptions(View view,int position,String optionType);
    }


    /*
     * Holder
     */
    public class Holder extends RecyclerView.ViewHolder {

        SwipeLayout sl_item;//侧滑控件
        LinearLayout ll_main;
        LinearLayout ll_back;

        TextView tv_item_edit;//编辑按钮
        TextView tv_item_delete;//删除按钮

        TextView timeTV;
        TextView value1TV;
        ImageView picIV;




        public Holder(View view) {
            super(view);
            timeTV = (TextView) view.findViewById(R.id.chartdetail_tv_time);
            value1TV = (TextView) view.findViewById(R.id.chartdetail_tv_value1);
            picIV = (ImageView) view.findViewById(R.id.chartdetail_iv_pic);

            sl_item = (SwipeLayout) view.findViewById(R.id.sl_item);
            tv_item_edit = (TextView) view.findViewById(R.id.tv_item_edit);
            tv_item_delete = (TextView) view.findViewById(R.id.tv_item_delete);
            ll_main = (LinearLayout) view.findViewById(R.id.ll_main);
            ll_back = (LinearLayout) view.findViewById(R.id.ll_back);

        }
    }

    private String getDateFormat(String sourDateFormat,String sourDate) {
        // 分析从 ParsePosition 给定的索引处开始的文本。如果分析成功，则将 ParsePosition 的索引更新为所用最后一个字符后面的索引
        ParsePosition position = new ParsePosition(0);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(sourDateFormat);
        Date dateValue = simpleDateFormat.parse(sourDate, position);
        String returnString = simpleDateFormat.format(dateValue);
        return returnString;
    }
}
